package Day3.ZuoYe;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

//4:baidu ArrayList和LinkedList实现原理，并比较性能（操作加入新值100000次，随机删除100个值，比较时间）
public class ZuoYe4 {
    public static void main(String[] args) {
        List a=new ArrayList();
        List b=new LinkedList();
        long a1=System.nanoTime();
        for(int i=0;i<100000; a.add(i),i++);
        System.out.println(a.size());
        for(int i=0;i<100;i++){
            int s= (int) (Math.random()*100000);
            a.remove(s);
        }
        System.out.println(a.size());
        long a2=System.nanoTime();
        long b1=System.nanoTime();
        for(int i=0;i<100000; b.add(i),i++);
        System.out.println(b.size());
        for(int i=0;i<100;i++){
            int s= (int) (Math.random()*100000);
            b.remove(s);
        }
        System.out.println(b.size());
        long b2=System.nanoTime();
        double aTime=a2-a1;
        double bTime=b2-b1;
        System.out.println("ArrayList的测试时间是"+aTime+"纳秒。");
        System.out.println("LinkedList的测试时间是"+bTime+"纳秒。");
        if (aTime>bTime) {
            System.out.println("ArrayList比LinkedList慢！");
        }else if(aTime==bTime){
            System.out.println("ArrayList比LinkedList一样快！");
        }else {
            System.out.println("ArrayList比LinkedList快！");
        }
    }
}
